<template>
    <div class="app-container flex-cloumn">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68">
            <el-form-item label="名称" prop="configName">
                <el-input
                        v-model="queryParams.reportName"
                        placeholder="请输入报告名称"
                        clearable
                        style="width: 240px"
                        @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="报告ID" prop="reportId">
                <el-input
                        v-model="queryParams.reportId"
                        placeholder="请输入报告ID"
                        clearable
                        style="width: 240px"
                        @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="测试日期">
                <el-date-picker
                    v-model="queryParams.calcDate"
                    type="date"
                    :style="{width: '100%'}"
                    value-format="YYYY-MM-DD"
                    @change="handleQuery"
                    placeholder="选择日期">
                </el-date-picker>
            </el-form-item>

            <el-form-item label="类型" prop="reportType" v-if="false">
                <el-select v-model="queryParams.reportType" placeholder="类型" clearable>
                    <el-option
                            v-for="dict in reportType"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button v-hasPermi="['testmanage:report:list']" type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button
                    type="danger"
                    icon="Delete"
                    :disabled="multiple"
                    @click="handleDelete"
                    v-hasPermi="['testmanage:report:remove']"
                >删除
                </el-button>
            </el-form-item>
        </el-form>

        <xTable ref="reportTableRef" refId="reportTableRef" :data="reportList" border 
            @selection-change="handleSelectionChange" v-loading="loading">
            <template #columList>
                <el-table-column label="报告ID" fixed align="left" prop="reportId" width="150" :show-overflow-tooltip="true"/>
                <el-table-column label="报告名称" align="left" prop="reportName" width="200" :show-overflow-tooltip="true"/>
                <el-table-column label="版本" align="left" prop="reportVersion" :show-overflow-tooltip="true"/>
                <el-table-column label="类型" align="center" prop="reportType" :show-overflow-tooltip="true">
                    <template #default="scope">
                        <dict-tag :options="reportType" :value="scope.row.reportType"/>
                    </template>
                </el-table-column>
                <el-table-column label="状态" align="center" prop="status" :show-overflow-tooltip="true">
                    <template #default="scope">
                        <dict-tag :options="reportStatus" :value="scope.row.status"/>
                    </template>
                </el-table-column>
                <el-table-column label="结果" align="left" width="150" >
                    <template #default="scope">
                        <el-progress v-if="scope.row.status == '0'" class="custom-progress" :show-text="false" color="#46D6A0" :percentage="scope.row.percentage"></el-progress>
                    </template>
                </el-table-column>
                <el-table-column label="测试日期" align="left" prop="calcDate"/>
                <el-table-column label="请求时间" align="center" prop="requestTime"  width="150" :show-overflow-tooltip="true"/>
                <el-table-column label="响应时间" align="center" prop="responseTime"  width="150" :show-overflow-tooltip="true"/>
                <el-table-column label="环境" align="left" prop="envName" width="230"  :show-overflow-tooltip="true"/>
                <el-table-column label="轮次" align="right" prop="round" :show-overflow-tooltip="true"/>
                <el-table-column label="并发数" align="right" prop="concurrency" :show-overflow-tooltip="true"/>
                <el-table-column label="总数" align="right" prop="count" :show-overflow-tooltip="true"/>
                <el-table-column label="失败数" align="right" prop="failCount" :show-overflow-tooltip="true"/>
                <el-table-column label="成功数" align="right" prop="successCount" :show-overflow-tooltip="true"/>
                <el-table-column label="成功率" align="right" prop="percentage" :show-overflow-tooltip="true">
                    <template #default="scope">
                        <span>{{ scope.row.percentage +"%" }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="耗时(ms)" align="right" prop="consumeTime" :show-overflow-tooltip="true"/>
                <el-table-column label="备注" align="left" prop="remark" :show-overflow-tooltip="true"/>
                <el-table-column label="创建时间" align="center" prop="createTime" width="180">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" align="right" width="55" class-name="small-padding fixed-width">
                    <template #default="scope">
                      <el-link type="primary"  :icon="Download" :underline="false" v-hasPermi="['testmanage:report:exportReport']" v-if="scope.row.status == '0'" style="font-size: 16px; vertical-align: middle;" @click.stop="handleExport(scope.row)" >
                        </el-link>
                      <el-link type="primary"  :icon="Delete" :underline="false" v-hasPermi="['testmanage:report:remove']" style="font-size: 16px; vertical-align: middle;" @click.stop="deleteReport(scope.row.reportId)"  class="ml5">
                        </el-link>
                    </template>
                </el-table-column>
            </template>
        </xTable>

        <pagination
                style="margin:20px 0 20px 0"
                v-show="total > 0"
                :total="total"
                v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize"
                @pagination="getList"
        />
    </div>
</template>

<script setup name="Report">
import {getCurrentInstance, reactive, ref} from "vue";
import { Delete,Download } from '@element-plus/icons-vue'
const {proxy} = getCurrentInstance();
import {listConfig} from "@/api/testmanage/report";

const {reportType, productType,calcType,optionType, reportStatus} = proxy.useDict("reportType", "productType","calcType","optionType", "reportStatus");
import {parseTime} from "../../../utils/xpp";
import {div, mul} from "@/utils/util";

import {
    deleteReports
} from "@/api/testmanage/autoTest";

const reportList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const multiple = ref(true);
const total = ref(0);
const dateRange = ref([]);


const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 20,
    }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询环境列表 */
function getList() {
    loading.value = true;
    listConfig(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
        reportList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}


/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    dateRange.value = [];
    proxy.resetForm("queryRef");
    handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.reportId);
    multiple.value = !selection.length;
}

/** 删除按钮操作 */
function handleDelete() {
    proxy.$modal.confirm('是否确认删除报告ID为"' + ids.value + '"的数据项？').then(function () {
        return deleteReports(ids.value);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {
    });
}

//移除单个报告
function deleteReport(id) {
    let ids = []
    ids.push(id)
    deleteReports(ids).then(response => {
        proxy.$modal.msgSuccess("删除成功!");
        getList()
    });
}

/** 导出按钮操作 */
function handleExport(row) {
    if (row.reportSource == 'autoTest') {
        proxy.download("/inf/exportAtReport", Object.assign({}, {reportId: row.reportId}));
    } else {
        proxy.download("/inf/exportReport", Object.assign({}, {reportId: row.reportId}));
    }
}

getList();
</script>

<style scoped>
/* 修改进度条的底色 */
/deep/.custom-progress .el-progress-bar__outer {
  background-color: #E95477;
}
</style>
